
var currency = '-';
var exchange = 1.0;

$(function() {

	$("#in_item").autocomplete({
		source: function( request, response ) {
			$.ajax({
				url: 'finance.php?do=InItem.ajax_code',
				dataType: "json",
				data: {
					featureClass: "P",
					style: "full",
					maxRows: 12,
					q: request.term
				},
				success: function( data ) {
					response( $.map( data, function( item ) {
						var s = item.in_x.name + item.name + ", " + item.emp_t;
						
						return {
							label: item.currency + " [" + item.amount + ", " + item.date + ", " + s + "]",
							value: item.id + ":" + item.emp_t
						}
					}));
				}
			});
		},
		
		minLength: 1
	});
	
	$('#in_item').blur(function() {
		var a = $(this).val();
		
		_in_item(a);
	});
			
	// _in_item($("#in_item").val());
	
	$("input[id^='emp']:visible").autocomplete(Test.emp);
		
	_currency($('#currency').val());
	
	$('#currency').change(function() {
		var s = $(this).val();
		_currency(s);
	});
	
	$('#income').keyup(function() {		
		_currency($("#currency").val());
	});
	
	$('#cost').keyup(function() {		
		_profit();
	});
	
	$('#rate').keyup(function() {		
		_rmb();
	});
		
	$('#date').datepicker(_date1_option);
	
	$('#cost_t').change(function() {
		var s = $(this).val();
		if (s != null && s != '-')　{
			var a = $('#rmb').val();
			var cost = fxmul(a, s);
			
			cost  = fmoney(cost, 2);
			cost = cost.replace(/,/g, '');
			
			$('#cost').val(cost);
			
			_profit();
		}
	});
	
	$('#percent_t').change(function() {
		var s = $(this).val();
		if (s != null && s != '-')　{
			$('#percent').val(s);
		}
		
		_amount();
	});
	
	$('#percent').keyup(function() {	
		_amount();
	});
	
	$('#type2').change(function() {
		_amount();		
	});
		
	$('textarea').css("width", 400);	
});

function _in_item(a) {
	// alert(a);
	if (a == null || a == '') {
		return;
	}
	
	var body = { };
	body.id = a;
	$.getJSON('finance.php?do=InItem.ajax_id', body, function(json) {
		// alert(json);
		var s = json.in_x.name + json.name;
		var name = $("#name").val();
		if (name != s) {
			$("#name").val(name + s);
		}			
		
		// $("#emp").val(json.emp);
		$("#type").attr("value", json.type);
		$("#income").val(json.amount);			
		$("#currency").attr("value", json.currency);
		if (!is_null(json.cost)) {
			$("#cost").val(json.cost);
		}
		
		if (!is_null(json.profit)) {
			$("#profit").val(json.profit);
		}		
		
		$("#date").val(json.date);
		
		_currency($('#currency').val());
	});
}

function _currency(s) {
	if (s == null || s == '-')　{		
		return false;
	}
	
	var body = { };
	body.currency = s;
	$.post('finance.php?do=Finance.ajax_exchange', body, function(s) {
		exchange = s;
		$('#rate').val(s);
		
		_update();
	});
}

function _update() {
	var a = $("#income").val();
	var b = $("#currency").find("option:selected").text();

	Currency.upper(a, b, "#label_income");
	
	_rmb();
} 

function _rmb() {
	var a = $("#income").val();
	var b = $('#rate').val();	
	if (is_null(a) || is_null(b)) {
		return;
	}
	
	var rmb = fxmul(a, b);
	
	rmb  = fmoney(rmb, 2);
	rmb = rmb.replace(/,/g, '');
	
	$('#rmb').val(rmb);
	
	_profit();
}

function _profit() {
	var a = $("#rmb").val();
	a = a.replace(/,/g, '');
	// alert(a);
	
	var b = $('#cost').val();
	if (is_null(a) || is_null(b)) {
		return;
	}
	
	var profit = a - b;
	
	profit  = fmoney(profit, 2);
	profit = profit.replace(/,/g, '');
	
	$('#profit').val(profit);
	
	_amount();
}

function _amount() {
	var type = $("#type2").val();
	
	var c = $('#percent').val();
	if (is_null(c) || is_null(type)) {
		return false;
	}
	
	var amount = 0;
	if (type == '1') {
		var a = $("#rmb").val();
		a = a.replace(/,/g, '');
		
		if (is_null(a)) {
			return;
		}
		
		amount = fxmul(a, c);
		amount  = fmoney(amount, 2);
		amount = amount.replace(/,/g, '');
		
		$('#amount').val(amount);
	} else if (type == '2') {
		var b = $('#profit').val();
		b = b.replace(/,/g, '');
		if (is_null(c)) {
			return;
		}
		
		amount = fxmul(b, c);
		amount  = fmoney(amount, 2);
		amount = amount.replace(/,/g, '');
		
		$('#amount').val(amount);
	}		
}
